<?php

namespace App\Notifications;


use App\Channels\SmsChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Log;
use App\Models\AdminUser;
use App\Models\Project;
use App\Services\SmsService;

class ToPayNotify extends Notification implements ShouldQueue
{
    use Queueable;

    // 审批完以后的协议
    protected $payInfo;

    // 审批log
    protected $userInfo;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($pay, $user)
    {
        $this->payInfo = $pay;
        $this->userInfo = $user;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        // return ['mail', SmsChannel::class];
        return [SmsChannel::class];
    }


    /**
     * Get the array representation of the notification.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function toSms($notifiable)
    {
        Log::info('------ 发送短信 Send  ToPayNotify ------' . json_encode($notifiable));

        $message = [];
        // todo 判断状态发送审核成功、或者审核失败的短信。
        if ($this->payInfo['pay_status'] == 2) {
            // 成功
            $template = 'TOPAY_SUCCESS';
            $message = [$this->userInfo['realname']];
        } elseif ($this->payInfo['pay_status'] == -1) {
            $template = 'TOPAY_FAIL';
            $message = [$this->payInfo['audit_remark']];
        }

        Log::info(' ====== message ====== ' . json_encode($message));
        // 查找对应代理人手机号，给代理人发送短信通知
        $agent = AdminUser::where('id', $this->userInfo['agent'])->first();
        if (!empty($agent)) {
            $phone = $agent['phone'];

            SmsService::sendCode($phone, $message, $template);
            Log::info('Send  ToPayNotify End ' . $phone . json_encode($message));
        } else {
            return '';
        }
    }
}
